package com.zb.framework.wechatnews.controller;

import com.zb.framework.common.constant.StatusCode;
import com.zb.framework.common.entity.ResponseEntity;
import com.zb.framework.wechatnews.common.BizException;
import com.zb.framework.wechatnews.entity.*;
import com.zb.framework.wechatnews.entity.vo.*;
import com.zb.framework.wechatnews.service.SendMessageService;
import com.zb.framework.wechatnews.utils.CheckParamsUtil;
import com.zb.framework.wechatnews.utils.JsonUtil;
import com.zb.framework.wechatnews.utils.WeChatUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: 周作为
 * @Date: 2019/6/18 15:29
 * @Description:
 */
@Api(value = "企业微信消息推送模块")
@RestController
@RequestMapping({"/weChat"})
public class WeChatController {

    @Value("${enterprise.wechat.corpId}")
    private String CORPID;  //凭证

    @Value("${enterprise.wechat.corpsecret}")
    private String CORPSECRET; //密钥

    @Autowired
    private SendMessageService sendMessageService;



    @ApiOperation(value = "发送文本或markdown消息", notes = "发送文本或markdown消息")
    @PostMapping("/sendTextMsg")
    @ResponseBody
    public ResponseEntity sendTextMsg(@RequestBody TextMsg msg){
        try {
            AccessToken accessToken = WeChatUtil.getAccessToken(CORPID,CORPSECRET);
            CheckParamsUtil.checkBaseMessage(msg);
            String jsonMessage = JsonUtil.obj2String(msg);
            WeChatResult weChatResult = sendMessageService.sendMessage(accessToken.getToken(),jsonMessage);
            return ResponseEntity.build(StatusCode.OK,"",weChatResult);
        }catch (BizException e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,e.getMsg(),null);
        }catch (Exception e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,"文本消息推送失败",null);
        }
    }

    @ApiOperation(value = "发送图片媒体文件消息", notes = "发送图片媒体文件消息（图片、语音、视频、文件）")
    @PostMapping("/sendMediaMsg")
    @ResponseBody
    public ResponseEntity sendMediaMsg(@RequestBody MediaMsg msg){
        try {
            AccessToken accessToken = WeChatUtil.getAccessToken(CORPID,CORPSECRET);
            CheckParamsUtil.checkBaseMessage(msg);
            String jsonMessage = JsonUtil.obj2String(msg);
            WeChatResult weChatResult = sendMessageService.sendMessage(accessToken.getToken(),jsonMessage);
            return ResponseEntity.build(StatusCode.OK,"",weChatResult);
        } catch (BizException e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,e.getEnumsError().getMsg(),null);
        }catch (Exception e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,"图片媒体文件消息推送失败",null);
        }
    }

    @ApiOperation(value = "发送文本卡片消息", notes = "发送文本卡片消息")
    @PostMapping("/sendTextCard")
    @ResponseBody
    public ResponseEntity sendTextCard(@RequestBody TextCardMsg msg){
        try {
            AccessToken accessToken = WeChatUtil.getAccessToken(CORPID,CORPSECRET);
            CheckParamsUtil.checkBaseMessage(msg);
            String jsonMessage = JsonUtil.obj2String(msg);
            WeChatResult weChatResult = sendMessageService.sendMessage(accessToken.getToken(),jsonMessage);
            return ResponseEntity.build(StatusCode.OK,"",weChatResult);
        }catch (BizException e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,e.getEnumsError().getMsg(),null);
        }catch (Exception e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,"文本卡片消息推送失败",null);
        }
    }

    @ApiOperation(value = "发送图文消息", notes = "发送图文消息")
    @PostMapping("/sendArticlesMsg")
    @ResponseBody
    public ResponseEntity sendArticlesMsg(@RequestBody ArticlesMsg msg){
        try {
            AccessToken accessToken = WeChatUtil.getAccessToken(CORPID,CORPSECRET);
            CheckParamsUtil.checkBaseMessage(msg);
            String jsonMessage = JsonUtil.obj2String(msg);
            WeChatResult weChatResult = sendMessageService.sendMessage(accessToken.getToken(),jsonMessage);
            return ResponseEntity.build(StatusCode.OK,"",weChatResult);
        }catch (BizException e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,e.getEnumsError().getMsg(),null);
        }catch (Exception e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,"图文消息推送失败",null);
        }
    }

    @ApiOperation(value = "发送图文消息（mpnews）", notes = "发送图文消息（mpnews）")
    @PostMapping("/sendMpNews")
    @ResponseBody
    public ResponseEntity sendMpNews(@RequestBody MpNews msg){
        try {
            AccessToken accessToken = WeChatUtil.getAccessToken(CORPID,CORPSECRET);
            CheckParamsUtil.checkBaseMessage(msg);
            String jsonMessage = JsonUtil.obj2String(msg);
            WeChatResult weChatResult = sendMessageService.sendMessage(accessToken.getToken(),jsonMessage);
            return ResponseEntity.build(StatusCode.OK,"",weChatResult);
        }catch (BizException e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,e.getEnumsError().getMsg(),null);
        }catch (Exception e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,"图文消息（mpnews）推送失败",null);
        }
    }

    @ApiOperation(value = "发送小程序通知消息", notes = "发送小程序通知消息")
    @PostMapping("/sendMiniProgramMsg")
    @ResponseBody
    public ResponseEntity sendMiniProgramMsg(@RequestBody MiniProgramMsg msg){
        try {
            AccessToken accessToken = WeChatUtil.getAccessToken(CORPID,CORPSECRET);
            CheckParamsUtil.checkBaseMessage(msg);
            String jsonMessage = JsonUtil.obj2String(msg);
            WeChatResult weChatResult = sendMessageService.sendMessage(accessToken.getToken(),jsonMessage);
            return ResponseEntity.build(StatusCode.OK,"",weChatResult);
        }catch (BizException e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,e.getEnumsError().getMsg(),null);
        }catch (Exception e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,"小程序通知消息失败",null);
        }
    }

    @ApiOperation(value = "发送任务卡片消息", notes = "发送任务卡片消息（仅企业微信2.8.2及以上版本支持）")
    @PostMapping("/sendTaskCardMsg")
    @ResponseBody
    public ResponseEntity sendTaskCardMsg(@RequestBody TaskCardMsg msg){
        try {
            AccessToken accessToken = WeChatUtil.getAccessToken(CORPID,CORPSECRET);
            CheckParamsUtil.checkBaseMessage(msg);
            String jsonMessage = JsonUtil.obj2String(msg);
            WeChatResult weChatResult = sendMessageService.sendMessage(accessToken.getToken(),jsonMessage);
            return ResponseEntity.build(StatusCode.OK,"",weChatResult);
        }catch (BizException e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,e.getEnumsError().getMsg(),null);
        }catch (Exception e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,"任务卡片消息推送失败",null);
        }
    }



    @ApiOperation(value = "更新任务卡片消息状态", notes = "更新任务卡片消息状态")
    @PostMapping("/updateTaskCard")
    @ResponseBody
    public ResponseEntity updateTaskCard(@RequestBody UpdateTaskCard record){
        try {
            AccessToken accessToken = WeChatUtil.getAccessToken(CORPID,CORPSECRET);
            WeChatResult weChatResult = sendMessageService.updateTaskCard(accessToken.getToken(),record);
            return ResponseEntity.build(StatusCode.OK,"",weChatResult);
        }catch (Exception e){
            return ResponseEntity.build(StatusCode.SERVER_ERROR,e.getMessage(),null);
        }
    }

}
